package com.zzh.structuredeployment.frag

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.zzh.custom.chess.ChessBoardView
import com.zzh.custom.chess.PiecesPerformView
import com.zzh.structuredeployment.R
import com.zzh.tools.utils.ToolBundle

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2021-06-01
 * @desc
 */
class SwatchesFragment : Fragment() {
    private val TAG = SwatchesFragment::class.java.simpleName

    companion object {


        fun newInstance(colorStr: String): SwatchesFragment {
            val fragment = SwatchesFragment()
            fragment.arguments = ToolBundle.createBundle("color", colorStr)
            return fragment
        }
    }

    private var mRootView: View? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_swatches, null)
        }
        initOther()
        return mRootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val chessBoardView = view.findViewById<ChessBoardView>(R.id.mChessBoardView)
        val piecesPerformView = view.findViewById<PiecesPerformView>(R.id.mPiecesPerformView)
        chessBoardView.setOnPointClickListener { key, pointF ->
            if (piecesPerformView.selectKey <= 0) {
                piecesPerformView.selectKey = key
            } else if (piecesPerformView.selectKey == key) {
                piecesPerformView.selectKey = -1
            } else {
                piecesPerformView.gotoKey(key)
            }
        }
        piecesPerformView.setOnLoadPointF {
            chessBoardView.getSpecifiedPointF(it)
        }
    }


    private fun initOther() {
        mRootView?.apply {
            findViewById<View>(R.id.swatchesView).setBackgroundColor(
                Color.parseColor(
                    arguments?.getString(
                        "color"
                    )
                )
            )
        }

    }


}